<?php

namespace Biz\service\Album;

use Biz\model\Album\AlbumWikiListModel;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient as tHhzClient;


class WikiListService
{

    const PAGESIZE = 50;


    public static function getGoodsList($album_id, $page)
    {
        $goodsList = AlbumWikiListModel::selectByWhere([
            'album_id' => $album_id,
            'LIMIT' => [($page - 1) * self::PAGESIZE, self::PAGESIZE],
            'status' => AlbumWikiListModel::STATUS_NORMAL,
            'ORDER' => ['sort' => 'ASC', 'addtime' => 'DESC']
        ]);
        if (empty($goodsList)) return [];
        $return = [];
        if ($goodsList && is_array($goodsList)) {
            $objIds = array_column($goodsList, 'obj_id');
            $objIds = array_filter(array_unique($objIds));
            $wikiIds = array_column($goodsList, 'wiki_id');
            $contentInfo = $objIds ? self::getConentInfo($objIds) : [];
            $wikiInfo = self::getWikiInfo($wikiIds);

            foreach ($goodsList as $key => $val) {
                if (empty($wikiInfo[$val['wiki_id']])) continue;
                $ObjInfo = [];
                if ($contentInfo[$val['obj_id']] && is_array($contentInfo[$val['obj_id']])) {
                    $ObjInfo = $contentInfo[$val['obj_id']];
                }
                $ObjInfo['remark'] = $val['content'] ?? $ObjInfo['remark'];
                $ObjInfo['obj_id'] = $val['obj_id'] ?? '';
                $return[] = [
                    'id' => $val['id'],
                    'wiki_info' => $wikiInfo[$val['wiki_id']],
                    'content_info' => $ObjInfo ?: [],
                    'statSign' => basicStatSign::genStatSign(basicStatSign::ALBUM_DETAIL_PAGE_WIKI_CARD, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $val['wiki_id'],
                        basicStatSign::EXTEND_KEY_RELATE_OBJ_TYPE => BlockType::TYPE_WIKI_ALBUM,
                        basicStatSign::EXTEND_KEY_RELATE_OBJ_ID => $album_id,
                        basicStatSign::EXTEND_KEY_INDEX => $key,
                    ], []),

                ];
            }
        }
        return $return;
    }


    public static function getConentInfo($obj_ids)
    {
        $noteInfo = HandleNote::handleNote($obj_ids, HandleNote::BASE_INFO);
        $result = [];
        if ($noteInfo && is_array($noteInfo)) {
            foreach ($noteInfo as $key => $val) {
                $userInfo = $val['photo']['user_info'];
                $result[$key]['avatar'] = $userInfo['small_avatar'];
                $result[$key]['nick'] = $userInfo['nick'];
                $result[$key]['remark'] = $val['photo']['photo_info']['remark'];
                $result[$key]['obj_id'] = $key;

            }
        }
        return $result;
    }

    public static function getWikiInfo($wikiIds)
    {
        $wikiInfo = sWiki::getWikiList($wikiIds, tHhzClient::getUid(), sWiki::FIELDS_GROUP_ALBUM_DETAIL_WIKI_LIST);
        $result = [];
        if ($wikiInfo && is_array($wikiInfo)) {
            foreach ($wikiInfo as $key => $val) {
                $result[$key]['id'] = $val['id'];
                $result[$key]['basic_id'] = $val['basic_id'];
                $result[$key]["basic_title"] = $val['title'];
                $result[$key]["basic_state"] = $val['basic_state'];
                $result[$key]["title"] = $val['title'];
                $result[$key]["ori_min_price"] = $val['ori_min_price'];
                $result[$key]["ori_max_price"] = $val['ori_max_price'];
                $result[$key]["cover_img"] = $val['cover_img'];
                $result[$key]["is_fav"] = $val['is_fav'];
            }
        }
        return $result;
    }


}